package concurrency_design_model

import (
	"fmt"
	"time"
)

func main() {
	//创建两个管道
	intchan := make(chan int, 100)
	exitchan := make(chan bool, 1)

	go writeData(intchan)
	go readData(intchan, exitchan)

	for {
		fmt.Println("等待读写完成，关闭主线程")
		v, ok := <-exitchan
		if !ok {
			break
		}
		fmt.Println("intchan读取完毕", v)
	}
}

//写数据
func writeData(intchan chan int) {
	for i := 0; i < 50; i++ {
		//休眠1秒钟
		time.Sleep(time.Second / 10)
		fmt.Println("写数据")
		intchan <- i
	}
	close(intchan)
}

//读取数据
func readData(intchan chan int, exitchan chan bool) {
	for {
		v, ok := <-intchan
		if !ok {
			break
		}
		fmt.Println("读到数据=", v)
	}
	exitchan <- true
	close(exitchan)
}
